package com.gupt.reggie.controller;/*
@author name
@data 2023/4/24-23:13
*/

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gupt.reggie.common.R;
import com.gupt.reggie.dto.SetmealDto;
import com.gupt.reggie.entity.Setmeal;
import com.gupt.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private RedisTemplate redisTemplate;



    @PostMapping
    @CacheEvict(value = "SetmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("套餐数据：{}", setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        Page<SetmealDto> dtoPage = setmealService.page(page,pageSize,name);
        return R.success(dtoPage);
    }

    @DeleteMapping
    @CacheEvict(value = "SetmealCache",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids) {
        log.info("删除的套餐为：{}", ids);
        setmealService.removeWithDish(ids);
        return R.success("删除成功");
    }

    @Cacheable(value = "SetmealCache" ,key = "#setmeal.categoryId + '_' + #setmeal.getStatus()")//由查询条件组成key值
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
//        List<Setmeal> list = null;
//        //构造key的唯一标识
//        String key = "setmeal_" + setmeal.getCategoryId() + "_" + setmeal.getStatus();
//
//        //查询redis中有无缓存该分类的商品
//        list = (List<Setmeal>)redisTemplate.opsForValue().get(key);
//        //有缓存则直接返回菜品数据
//        if(list != null){
//            return  R.success(list);
//        }

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);

//        //查不到则 从数据库返回后存入redis
//        redisTemplate.opsForValue().set(key,list,60, TimeUnit.MINUTES);
        return R.success(list);
    }

    /**
     * 修改套餐数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> list(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.getByIdWithFlavor(id);
        return R.success(setmealDto);
    }

    /**
     * 保存套餐修改
     * @param setmealDto
     * @return
     */
    @PutMapping
    @CacheEvict(value = "SetmealCache",allEntries = true)
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }

    /**
     * 修改套餐状态
     * @param id
     * @param ids
     * @return
     */
    @PostMapping("/status/{id}")
    @CacheEvict(value = "SetmealCache",allEntries = true)
    public R<String> save(@PathVariable Integer id,@RequestParam List<Long> ids){
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(id);
        // UPDATE setmeal  SET status = 'id' WHERE id in ( 'ids','ids');
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        setmealService.update(setmeal,queryWrapper);
//
//        //查询套餐的分类id
//        List<Setmeal> list = setmealService.list(queryWrapper);
//        for(Setmeal item:list){
//            Long categoryId = item.getCategoryId();
//            //精确清理某一个类型的缓存数据
//            String key = "setmeal_" + categoryId + "_1";
//            redisTemplate.delete(key);
//        }
        return R.success("修改成功");
    }
}